package coms;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class Connecting extends Socket {
	
	Socket mySocket = null;
	PrintWriter out = null;
    BufferedReader in = null;
    
    
    protected void SetUpPrintWriterAndBufferedReader() {
    	/**
    	 * This method sets up the input and output for server/client. It's called
    	 * when you run the constructor of ConnectingClient or ConnectingServer, so after
    	 * creating a server/client, you can communicate to the client/server using
    	 * writeToSocket to write to the socket, and getBufferedReader to handle the output.
    	 */
    	try {
			this.out = new PrintWriter(mySocket.getOutputStream(), true);
			this.in = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
    public void writeToSocket(String s) {
    	/**
    	 * This method allows one to write to the socket,
    	 * so it might be read at the other end.
    	 */
    	out.write(s);
    }
    
    public BufferedReader getBufferedReader() {
    	/**
    	 * This method returns a BufferedReader that you can use to handle the output of
    	 * the server/client. The most useful command for BufferedReader(s) is readLine,
    	 * which reads the next line of the buffer. It's easier to use if the input was
    	 * ended with '\n'.
    	 */
    	return this.in;
    }

	public void closeConnection() {
		/**
		 * This function closes all the streams and the connection to the server/client.
		 */
		try {
			out.close();
			in.close();
			mySocket.close();
		}
		catch (IOException e) {
			System.out.println(e);
		}
	}
	
	
	
	public boolean isConnected() {
		return mySocket.isConnected();
	}

	
}
